// 获取路径
var pathName = window.document.location.pathname;
// 截取，得到项目名称
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);


// 角色添加界面的全选
$('.type-item label').click(function() {
    // var length = $(this).parents('.quanxian-content-item-main').children(".type-item").length;
    if($(this).hasClass('checked')) {
        $(this).removeClass('checked')
    }else{
        $(this).addClass('checked')
        $(this).parents('.quanxian-content-item-main').siblings('.quanxian-content-item-title').find('label').addClass('checked');
    }
    var checkedLength = $(this).parents('.quanxian-content-item-main').find('label[class="checked"]').length
    if(checkedLength === 0) {
        $(this).parents('.quanxian-content-item-main').siblings('.quanxian-content-item-title').find('label').removeClass('checked');
    }
})

$('.quanxian-content-item-title label').click(function() {
	if($(this).hasClass('checked')) {
        $(this).removeClass('checked')
        $(this).parent().siblings().find('label').removeClass('checked')
    }else{
        $(this).addClass('checked')
        $(this).parent().siblings().find('label').addClass('checked')
    }
})

//提交添加的角色按钮
function test(){
	var arrPrivilegeIds = [];
	var rolename = $("#rolename").val();
	var remark = $("#remark").val();
	
	$(".checked").each(function() {
		arrPrivilegeIds.push($(this).data('value'));
	})
	if(!rolename ){
		showDialog("必须填写一个角色名");
		return false;
	}
	if(arrPrivilegeIds.length == 0){
//		console.log(arr);
		showDialog("必须勾选至少一个权限");
		return false;
	}
	console.log(arrPrivilegeIds);
	var url = projectName+"/wxRoleAction_addRolePrivilege.action";
	var data = {
			'rolename':rolename,
			'remark':remark,
			arrPrivilegeIds:arrPrivilegeIds
	}

	$.ajax({
        type : "POST",
        url : url,
        dataType : "json",
        traditional : true,
        data : data,
        success : function(flag) {
        	if(flag == 1){
        	return showDialogsucurl("操作成功!用户重新登录后生效", projectName + "/wxAdminAction_findAll.action");
       
        	}else{
        		return showDialogsucur2("添加失败或权限不足");
        	}
        	}
        });
	
//			return showDialogsucurl("登录成功", projectName + "/index.jsp");
//			return window.location=projectName + "/wxRoleAction_findAll.action";;
}


$(document).on('click', '#button-bound', function() {
    $('.allot-bound-msk').fadeIn(400);
})
$(document).on('click', '.bound-msk-close', function() {
    $('.allot-bound-msk').fadeOut(400);
})
//编辑框弹出
function editRoleAndAdmin(wxAdmin_id,wxAdmin_truename,wxAdmin_wxRole_id,wxAdmin_wxRole_rolename) {
	console.log(wxAdmin_truename);
	console.log(wxAdmin_wxRole_id);
	$("#setRole-id").val(wxAdmin_id);
	$("#setRole-truename").val(wxAdmin_truename);
	$("#setRole-roleid option[value='"+wxAdmin_wxRole_id+"']").attr("selected", true);
//	'${wxAdmin.id }','${wxAdmin.truename }','${wxAdmin.wxRole.id }','${wxAdmin.wxRole.rolename }'
    //打开浮沉
	$('.allot-redact-msk ').fadeIn(400);
}
$(document).on('click', '.redact-msk-close', function() {
    $('.allot-redact-msk').fadeOut(400);
})

//删除角色    清空角色和权限的关系  再删除角色
function deleteRoleAndPrivilege(){
	var delRoleid = $("#delRole-roleid").find("option:selected").val();  //获取Select选择的Text 
	var url = projectName+"/wxRoleAction_deleteRoleAndPrivilege.action?delRoleid="+delRoleid;
//	showDialogConfirm("确认删除角色吗?", url);
	layer.open({
        content : "确认要删除角色吗?",
        icon:3,
        btn : ['是','否'],
        yes : function(){
        	$.post(url,function(result) {
        		if (result == "0") {
        			return showDialog("删除失败或者权限不足");
        		} else if (result == "1") {
//        			return showDialogsucurl("登录成功", projectName + "/index.jsp");
        			return showDialogsucur2("删除成功");
        		}
        	});
        },
    })
	
}


